import { all, call, put, select, takeLatest } from 'redux-saga/effects'
import api from '@API'
import {
  GET_EXPERT_FEEDS,
  GET_EXPERT_BANNER,
  GET_EXPERT_SPECIAL_TOPIC,
  GET_POPULAR_SEARCH,
  GET_USER_MATCHED_TAGS,
  GET_EXPERT_GOODS,
  GET_EXPERT_TEMPLATES,
} from '@ACTIONS/expert'
import { ExpertActionITF, TagIdsActionITF } from '@REDUXITF/expert'
import { getExpertFeedList } from '@SAGAS/dataSelect'

const getExpertFeeds = function* getExpertFeeds(action: ExpertActionITF) {
  try {
    const { payload = {} } = action
    const { skip, ...params } = payload
    const list = yield select(getExpertFeedList)
    const realSkip = skip === 0 ? 0 : list.length
    const data = yield call(api.getMatchedNewsFeed, {
      ...params,
      skip: realSkip,
      take: 10,
    })
    yield put({ type: GET_EXPERT_FEEDS.SUCCESS, data, skip: realSkip })
  } catch (error) {
    yield put({ type: GET_EXPERT_FEEDS.FAILURE, error })
  }
}

const getExpertSpecialTopic = function* getExpertSpecialTopic() {
  try {
    const result = yield call(api.getEventsListBySpotCode, { code: 'HomeSpecialTopic' })
    yield put({ type: GET_EXPERT_SPECIAL_TOPIC.SUCCESS, data: result })
  } catch (error) {
    yield put({ type: GET_EXPERT_SPECIAL_TOPIC.FAILURE, error })
  }
}

const getExpertBanner = function* getExpertBanner() {
  try {
    const result = yield call(api.getEventsListBySpotCode, { code: 'HomeBanner' })
    yield put({ type: GET_EXPERT_BANNER.SUCCESS, data: result })
  } catch (error) {
    yield put({ type: GET_EXPERT_BANNER.FAILURE, error })
  }
}

const getPopularSearch = function* getPopularSearch() {
  try {
    const result = yield call(api.getEventsListBySpotCode, { code: 'PopularSearch' })
    yield put({ type: GET_POPULAR_SEARCH.SUCCESS, data: result })
  } catch (error) {
    yield put({ type: GET_POPULAR_SEARCH.FAILURE, error })
  }
}

const getUserMatchedTags = function* getUserMatchedTags() {
  try {
    const result = yield call(api.getUserMatchedTags, {})
    yield put({ type: GET_USER_MATCHED_TAGS.SUCCESS, data: result })
    if (Array.isArray(result)) {
      const tIds = JSON.stringify(result || [])
      yield put({ type: GET_EXPERT_GOODS.REQUEST, tIds })
      yield put({ type: GET_EXPERT_TEMPLATES.REQUEST, tIds })
      yield put({ type: GET_EXPERT_FEEDS.REQUEST, payload: { skip: 0, tIds } })
    }
  } catch (error) {
    yield put({ type: GET_USER_MATCHED_TAGS.FAILURE, error })
  }
}

const getExpertGoods = function* getExpertGoods(action: TagIdsActionITF) {
  try {
    const { tIds } = action
    const { GoodsList } = yield call(api.getMatchedGoods, { take: 3, skip: 0, tIds })
    yield put({ type: GET_EXPERT_GOODS.SUCCESS, data: GoodsList })
  } catch (error) {
    yield put({ type: GET_EXPERT_GOODS.FAILURE, error })
  }
}

const getExpertTemplates = function* getExpertTemplates(action: TagIdsActionITF) {
  try {
    const { tIds } = action
    const { Templates } = yield call(api.getMatchedSurveyTemplates, { skip: 0, take: 3, tIds })
    yield put({ type: GET_EXPERT_TEMPLATES.SUCCESS, data: Templates })
  } catch (error) {
    yield put({ type: GET_EXPERT_TEMPLATES.FAILURE, error })
  }
}

export default function* rootSaga(): Generator {
  yield all([
    takeLatest(GET_EXPERT_FEEDS.REQUEST, getExpertFeeds),
    takeLatest(GET_POPULAR_SEARCH.REQUEST, getPopularSearch),
    takeLatest(GET_EXPERT_BANNER.REQUEST, getExpertBanner),
    takeLatest(GET_EXPERT_SPECIAL_TOPIC.REQUEST, getExpertSpecialTopic),
    takeLatest(GET_USER_MATCHED_TAGS.REQUEST, getUserMatchedTags),
    takeLatest(GET_EXPERT_GOODS.REQUEST, getExpertGoods),
    takeLatest(GET_EXPERT_TEMPLATES.REQUEST, getExpertTemplates),
  ]);
}
